﻿using ETA.ClientService;
using ETA.DomainEnum;
using ETA.Entity;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    public class SOSController : BaseController
    {
        //
        // GET: /SOS/
        private static IAskForHelpService _IAskForHelpService { set; get; }
        private static IUserService _IUserService { set; get; }

        public SOSController(IUserService service, IAskForHelpService IAskForHelpService)
        {
            _IUserService = service;
            _IAskForHelpService = IAskForHelpService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddHelpRequest(AddHelpReqDTO dto)
        {
            var res = _IAskForHelpService.CreateAskForHelp(
               new CreateAskForHelpRequest
               {
                   AskForHelp = new Entity.AskForHelp
                   {
                       AskForHelpContent = dto.Content,
                       SubmitUser = UserBll.GetCompltetUserInfo(CurrentUser.UserId),
                       AskForHelpId = Guid.NewGuid().ToString(),
                       AskForHelpKindEnum = (DomainEnum.AskForHelpKindEnum)(Int32.Parse(dto.Category)),
                       AskForHelpTypeEnum = (DomainEnum.AskForHelpTypeEnum)(Int32.Parse(dto.Type)),
                       CreateDate = DateTime.Now
                   }
               }
            );
            if (res.Result.IsSuccess)
            {
                TempData["Info"] = "true";
            }
            else
            {
                TempData["Info"] = res.Result.ErrMessage;
            }
            return RedirectToAction("Index");
        }

        public ActionResult GetSOSList(int page, int pageSize = 10)
        {
            var count = 0;
            var list = new List<AddHelpReqDTO>();
            var response = _IAskForHelpService.QueryAskForHelpList(
            new QueryAskForHelpRequest
            {
                UserId = CurrentUser.UserId,
                Page = page,
                PageSize = pageSize
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = new List<AddHelpReqDTO>();
                foreach (var a in response.AskForHelpList)
                {
                    list.Add(ConverntHelpDto(a));
                }
            }
            return JsonWithDate(list);
        }

        public ActionResult GetSOSById(string sosId)
        {
            AddHelpReqDTO sos = null;
            var count = 0;
            var list = new List<AddHelpReqDTO>();
            var response = _IAskForHelpService.QueryAskForHelpList(
            new QueryAskForHelpRequest
            {
                Sosid = sosId
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = new List<AddHelpReqDTO>();
                foreach (var a in response.AskForHelpList)
                {
                    list.Add(ConverntHelpDto(a));
                }
            }
            sos = list.FirstOrDefault();
            return JsonWithDate(sos);
        }

        /// <summary>
        /// 援助数据转换
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private AddHelpReqDTO ConverntHelpDto(AskForHelp model)
        {
            var dto = new AddHelpReqDTO
            {
                Account = model.SubmitUser.UserId,
                Category = model.AskForHelpKindEnum + "",
                Type = model.AskForHelpTypeEnum + "",
                Content = model.AskForHelpContent,
                Comment = model.EvaluateEnum + "",
                Date = model.CreateDate.ToShortDateString(),
                RecordId = model.AskForHelpId,
                Status = model.StateEnum + ""
            };
            return dto;
        }

        public ActionResult GetSOSCount(int page = 0, int pageSize = 10)
        {
            var count = 0;
            var response = _IAskForHelpService.QueryAskForHelpList(
            new QueryAskForHelpRequest
            {
                UserId = CurrentUser.UserId,
                Page = page,
                PageSize = pageSize
            }, out count
            );
            return JsonWithDate(count);
        }


        public ActionResult CommentSos(string id, int comment)
        {
            var response = _IAskForHelpService.CommentAskForHelp(id, (EvaluateEnum)comment);
            return JsonWithDate(response);
        }

    }

    public class AddHelpReqDTO
    {
        /// <summary>
        /// 用户id
        /// </summary>
        public string Account { set; get; }

        /// <summary>
        /// 事务类型 0 求援
        /// </summary>
        public string Type { set; get; }

        /// <summary>
        /// 事物分类 0 生活类 1工作类 2法律纠纷
        /// </summary>
        public string Category { set; get; }
        public string Content { set; get; }

        /// <summary>
        /// 评价 0 未评价 1 满意 2 一般 3不满意
        /// </summary>
        public string Comment { set; get; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public string Date { get; set; }

        /// <summary>
        /// 援助记录ID
        /// </summary>
        public string RecordId { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; }

    }
}
